<?php

class Upload extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->helper(array('form', 'url'));
	}

	function index()
	{
		$this->load->view('upload_form', array('error' => ' ' ));
	}

	function do_upload()
	{
		   $status = "";
		   $msg = "";
		   $file_element_name = 'userfile';
		   
		   if ($status != "error")
		   {
		      $config['upload_path'] = './upload/';
		      $config['allowed_types'] = 'gif|jpg|png|doc|txt';
		      $config['max_size']  = 1024 * 8;
		      $config['encrypt_name'] = TRUE;
		      $this->load->library('upload', $config);
		      if (!$this->upload->do_upload($file_element_name))
		      {
		         $status = 'error';
		         $msg = $this->upload->display_errors('', '');
		      }
		      else
		      {
		         $data = $this->upload->data();
		         
		         if($data['file_name'])
		         {
		         	$_SESSION['upload_file'] = $data['file_name'];
		            $status = "success";
		            $msg = "File successfully uploaded";
		         }
		         else
		         {
		            unlink($data['full_path']);
		            $status = "error";
		            $msg = "Something went wrong when saving the file, please try again.";
		         }
		      }
		      @unlink($_FILES[$file_element_name]);
		   }
		   //echo json_encode(array('status' => $status, 'msg' => $msg));
	}
}
?>